package jjn.round1;

import commons.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Jjn
 * @since 2022/9/18 08:02
 */
public class LeetCode513_FindBottomLeftTreeValue {

    public int findBottomLeftValue(TreeNode root) {
        if (root == null) {
            return -1;
        }
        int val = -1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();
            if (poll != null) {
                TreeNode left = poll.left;
                TreeNode right = poll.right;
                if (right != null) {
                    queue.offer(right);
                }
                if (left != null) {
                    queue.offer(left);
                }
                val = poll.val;
            }
        }
        return val;
    }
}
